package MyThread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: AliceMana
 * Date: 2023-01-02
 * Time: 15:10
 */
public class ThreadDemo17 {
    public static void main(String[] args) throws InterruptedException {
        Object object = new Object();

        Thread t1= new Thread(() -> {
            System.out.println("t1: wait 之前");
            try {
                synchronized (object) {
                    object.wait();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("t1: wait 之后");
        });

        Thread t2 = new Thread(() -> {
            System.out.println("t2: notify 之前");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            synchronized (object) {
                // notify 务必要获取到锁之后, 才能进行通知
                object.notify();
            }
            System.out.println("t2: notify 之后");
        });

        t1.start();
        Thread.sleep(500);
        t2.start();
    }
}
